package com.qdlc.p2p.dal.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.constant.TradeStatus;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.dao.TppTradeLogDao;
import com.qdlc.p2p.dal.dto.TppTradeLog;

/**
 * 第三方 代收/代付 交易记录
 *
 * @author zlhx
 * @version 2.0
 * @date 2015年8月13日 下午4:15:57
 */
@Repository("tppTradeLogDao")
public class TppTradeLogDaoImpl extends BaseDaoImpl<TppTradeLog> implements
        TppTradeLogDao {

    @SuppressWarnings("unchecked")
    @Override
    public TppTradeLog find(String orderNo) {
        String jpql = " from TppTradeLog where orderNo =?1";
        Query query = em.createQuery(jpql);
        query.setParameter(1, orderNo);
        List<TppTradeLog> list = query.getResultList();
        if (null != list && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TppTradeLog> find(long borrowRepaymentId, String serviceType,
                                  String tradeType) {
        String jpql = " from TppTradeLog where borrowRepaymentId =?1 and serviceType = ?2 and tradeType = ?3";
        Query query = em.createQuery(jpql);
        query.setParameter(1, borrowRepaymentId);
        query.setParameter(2, serviceType);
        query.setParameter(3, tradeType);
        List<TppTradeLog> list = query.getResultList();
        return list;
    }

    @Override
    public List<TppTradeLog> findRepayOrderByTradeCode(long borrowRepaymentId, int tradeCode) {
        String sql = " from TppTradeLog where borrowRepaymentId =:borrowRepaymentId and tradeType = :tradeType";
        Query query = em.createQuery(sql);
        query.setParameter("borrowRepaymentId", borrowRepaymentId);
        query.setParameter("tradeType", tradeCode);
        return query.getResultList();
    }

    @Override
    public boolean canReCreateOrder(long borrowId, long borrowRepaymentId, int tradeCode) {
        String sql = " from TppTradeLog where borrowId = :borrow_id and borrowRepaymentId =:borrow_repayment_id and tradeType = :trade_type "
                + " and (tradeStatus = :status1 "
                + " or tradeStatus = :status2 "
                + " or tradeStatus = :status3)";
        Query query = em.createQuery(sql);
        query.setParameter("borrow_id", borrowId);
        query.setParameter("borrow_repayment_id", borrowRepaymentId);
        query.setParameter("trade_type", String.valueOf(tradeCode));
        query.setParameter("status1", TradeStatus.TRADE_STATUS_WAIT_PAY.getStatus());
        query.setParameter("status2", TradeStatus.TRADE_STATUS_PAY_FINISHED.getStatus());
        query.setParameter("status3", TradeStatus.TRADE_STATUS_FINISHED.getStatus());
        return query.getResultList() == null || query.getResultList().size() == 0;
    }

    @Override
    public int updateTradeLog(TppTradeLog tppTradeLog) {
        StringBuilder sql = new StringBuilder("update ");
        sql.append(Global.DB_PREFIX).append("tpp_trade_log");
        sql.append(" set ");
        sql.append("  tpp_user_cust_id = :tpp_user_cust_id, ");
        sql.append("  identity_type = :identity_type, ");
        sql.append("  to_tpp_user_cust_id = :to_tpp_user_cust_id, ");
        sql.append("  money = :money, ");
        sql.append("  amount = :amount, ");
        sql.append("  order_no = :order_no, ");
        sql.append("  trade_status = :trade_status, ");
        sql.append("  trade_type = :trade_type, ");
        sql.append("  service_type = :service_type, ");
        sql.append("  borrow_id = :borrow_id, ");
        sql.append("  borrow_repayment_id = :borrow_repayment_id, ");
        sql.append("  split_list = :split_list, ");
        sql.append("  memo = :memo, ");
        sql.append("  add_time = :add_time, ");
        sql.append("  trade_time = :trade_time, ");
        sql.append("  sina_status = :sina_status, ");
        sql.append("  associate_order_id = :associate_order_id, ");
        sql.append("  remark = :remark ");
        sql.append(" where id = :id");
        Query query = em.createNativeQuery(sql.toString());
        query.setParameter("id", tppTradeLog.getId());
        query.setParameter("tpp_user_cust_id", tppTradeLog.getTppUserCustId());
        query.setParameter("identity_type", tppTradeLog.getIdentityType());
        query.setParameter("to_tpp_user_cust_id", tppTradeLog.getToTppUserCustId());
        query.setParameter("money", tppTradeLog.getMoney());
        query.setParameter("amount", tppTradeLog.getAmount());
        query.setParameter("order_no", tppTradeLog.getOrderNo());
        query.setParameter("trade_status", tppTradeLog.getTradeStatus());
        query.setParameter("trade_type", tppTradeLog.getTradeType());
        query.setParameter("service_type", tppTradeLog.getServiceType());
        query.setParameter("borrow_id", tppTradeLog.getBorrowId());
        query.setParameter("borrow_repayment_id", tppTradeLog.getBorrowRepaymentId());
        query.setParameter("split_list", tppTradeLog.getSplitList());
        query.setParameter("memo", tppTradeLog.getMemo());
        query.setParameter("add_time", tppTradeLog.getAddTime());
        query.setParameter("trade_time", tppTradeLog.getTradeTime());
        query.setParameter("sina_status", tppTradeLog.getSinaStatus());
        query.setParameter("associate_order_id", tppTradeLog.getAssociateOrderId());
        query.setParameter("remark", tppTradeLog.getRemark());
        return query.executeUpdate();
    }
}
